Skip to content

fix: reinitialize expired streamable sessions#2150

Open
he-yufeng wants to merge 2 commits into
modelcontextprotocol:mainfrom
he-yufeng:fix/streamable-http-session-expiry-reinit
Open

fix: reinitialize expired streamable sessions#2150
he-yufeng wants to merge 2 commits into
modelcontextprotocol:mainfrom
he-yufeng:fix/streamable-http-session-expiry-reinit

Conversation

@he-yufeng
Copy link
Copy Markdown

Summary

Fixes #1476.

When a Streamable HTTP server restarts, a client can keep sending POST requests with a stale MCP-Session-Id. The SDK currently reports the server's 404 response as ClientHttpNotImplemented and leaves the expired session in place.

This change handles a 404 from a request that used a session ID as an expired-session signal:

  • clear the cached session ID
  • let Client run a fresh initialize handshake on the same transport
  • retry the original message once with the new session ID

The retry is limited to one attempt so repeated 404s still surface as errors instead of looping.

To verify

  • pnpm --filter @modelcontextprotocol/client exec vitest run test/client/streamableHttp.test.ts -t "reinitializes and retries once"
  • pnpm --filter @modelcontextprotocol/client exec vitest run test/client/streamableHttp.test.ts
  • pnpm --filter @modelcontextprotocol/client typecheck
  • pnpm --filter @modelcontextprotocol/client lint
  • git diff --check
  • pre-push hook: full workspace typecheck, build, and lint

@he-yufeng he-yufeng requested a review from a team as a code owner May 24, 2026 09:48
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 24, 2026

🦋 Changeset detected

Latest commit: 80b3f60

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@modelcontextprotocol/client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 24, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@2150

@modelcontextprotocol/codemod

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/codemod@2150

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@2150

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@2150

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/fastify@2150

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@2150

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@2150

commit: 80b3f60

@he-yufeng
Copy link
Copy Markdown
Author

Added a patch changeset for @modelcontextprotocol/client. Local validation: pnpm exec changeset status --since upstream/main shows the client patch bump, git diff --check passed, and the pre-push hook completed typecheck, build, and lint successfully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Client received "no session ID or not initialization request" when server restart

1 participant